package com.legoo.studaychineseonline.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.legoo.studaychineseonline.R;
import com.legoo.studaychineseonline.adapter.MainViewPagerFragmentAdapter;
import com.legoo.studaychineseonline.base.BaseActivity;
import com.legoo.studaychineseonline.fragment.ActivityFragment;
import com.legoo.studaychineseonline.fragment.ClassFragment;
import com.legoo.studaychineseonline.fragment.HomeFragment;
import com.legoo.studaychineseonline.fragment.MyFragment;
import com.legoo.studaychineseonline.utils.BottomNavigationViewHelper;
import com.legoo.studaychineseonline.view.NoScrollViewPager;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * 主界面
 * */
public class MainActivity extends BaseActivity {
    @BindView(R.id.navigation)
    BottomNavigationView navigation;
    @BindView(R.id.vp)
    NoScrollViewPager viewPager;
    List<Fragment> fragmentList = new LinkedList<>();
    private long mExitTime;
    private MenuItem menuItem;
    private AlertDialog dialog;
    private MainViewPagerFragmentAdapter mainViewPagerFragmentAdapter;

    @Override
    protected void myOnCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);

        checkVersion();
    }


    @Override
    protected void initData() {
        mainViewPagerFragmentAdapter=null;
        fragmentList.clear();
        BottomNavigationViewHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_order:
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_zaixian:
                        viewPager.setCurrentItem(2);
                        return true;
                    case R.id.navigation_my:
                        viewPager.setCurrentItem(3);
                        return true;
                }
                return false;
            }
        });
        HomeFragment homeFragment = new HomeFragment();
        ClassFragment classFragment = new ClassFragment();
        ActivityFragment activityFragment = new ActivityFragment();
        MyFragment myFragment = new MyFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(classFragment);
        fragmentList.add(activityFragment);
        fragmentList.add(myFragment);
        mainViewPagerFragmentAdapter = new MainViewPagerFragmentAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setCurrentItem(0);
        viewPager.setAdapter(mainViewPagerFragmentAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    navigation.getMenu().getItem(0).setChecked(false);
                }
                menuItem = navigation.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void resolveBundle(Bundle bundle) {

    }

    @Override
    protected int myLayoutId() {
        return R.layout.activity_main;
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            //调用双击退出函数
            exitBy2Click();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 双击退出函数
     */


    private void exitBy2Click() {

        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
//            System.exit(0);

        }
    }

    private void checkVersion() {
        Map<String, Object> map = new HashMap<>();
        map.put("type", "android");

    }

    private void showUpdate() {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        dialog = adb.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_update, null);
        dialog.setView(view);
        TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                String data = "https://shouji.baidu.com/software/25649268.html";
                intent.setData(Uri.parse(data));
                startActivity(intent);
                dialog.dismiss();
            }
        });

//        tv_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//                System.exit(0);
//
////                dialog.dismiss();
//            }
//        });

        dialog.show();

    }

}
